#include "ShaderFactory.h"


ShaderFactory::ShaderFactory(void)
{
}


ShaderFactory::~ShaderFactory(void)
{
}

ShaderFactory* ShaderFactory::Instance()
{
	static ShaderFactory* instance = nullptr;

	if( nullptr == instance )
		instance = new ShaderFactory();

	return instance;
}

char* ShaderFactory::textFileRead(char *fn) {

	FILE *fp;
	char *content = NULL;

	int count=0;

	if (fn != NULL) {
		fp = fopen(fn,"rt");

		if (fp != NULL) {

			fseek(fp, 0, SEEK_END);
			count = ftell(fp);
			rewind(fp);

			if (count > 0) {
				content = (char *)malloc(sizeof(char) * (count+1));
				count = fread(content,sizeof(char),count,fp);
				content[count] = '\0';
			}
			fclose(fp);
		}
	}
	return content;
}

GLuint ShaderFactory::getShader( std::string fileName, GLenum shaderType )
{
	GLuint shader = glCreateShader( shaderType );
	const char* shaderText = textFileRead((char*)fileName.c_str())	;
	glShaderSource(shader, 1, &shaderText, NULL);
	glCompileShader(shader);
	return shader;
}
